﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Plugin.SourceEdit
{
    public partial class UCSourceEdit : UserControl
    {
       
        public UCSourceEdit()
        {
            InitializeComponent();
            //
            _supportlanguage.Sort();
            this.toolStripComboBox_Language.Items.AddRange(_supportlanguage.ToArray());
            this.toolStripComboBox_Language.SelectedText = "python";
            this.scintillaEdit.ConfigurationManager.Language = "python";
            minfontsize = this.scintillaEdit.Font.Size;
            this.scintillaEdit.MouseWheel += new MouseEventHandler(scintilla1_MouseWheel);
            scintillaEdit.AutoComplete.DropRestOfWord = true;
            scintillaEdit.AutoComplete.AutoHide = false;
            scintillaEdit.Indentation.SmartIndentType = ScintillaNET.SmartIndent.Simple;
            keywordList = new List<string>(keywords.Split(' ')); // manage your list dynamically (e.g. from a database), don't use the AutoComplete.List
            keywordList.Sort();
            scintillaEdit.CharAdded += new EventHandler<ScintillaNET.CharAddedEventArgs>(scintilla1_CharAdded);
        }

        void scintilla1_CharAdded(object sender, ScintillaNET.CharAddedEventArgs e)
        {
            if (e.Ch == ' ')
                return;
            int pos = scintillaEdit.NativeInterface.GetCurrentPos();
            string word = scintillaEdit.GetWordFromPosition(pos);
            if (word == string.Empty)
                return;
            
            List<string> list = keywordList.FindAll(item => item.StartsWith(word));
            if (list.Count > 0)
                scintillaEdit.AutoComplete.Show(list);
            if (e.Ch == '.')
            {

            }
        }
        private const string keywords = "_Title _Class _Love _Car _Parent _Child _Subclass _Data _Text";
        List<string> keywordList;
        List<string> _supportlanguage = new List<string>()
        {
            "ada","d","lot","props",
            "apdl","diff","lout","ps",
            "asm","eiffel","lua","purebasic",
            "asn1","eiffelkw","make","python",
            "au3","erlang","matlab","rebol",
            "ave","errorlist","metapost","ruby",
            "baan","escript","mmixal","scriptol",
            "bash","f77","mssql","smalltalk",
            "batch","flagship","nncrontab","specman",
            "blitsbasic","forth","nsis","spice",
            "bullant","fortran","null","sql",
            "caml","freebasic","octave","tads3",
            "clw","gap","opal","tcl",
            "clwnocase","gui4cli","pascal","tex",
            "cmake","haskell","perl","vb",
            "conf","hypertext","phpscript","vbscript",
            "cpp","innosetup","plm","verilog",
            "cppnocase","kix","pov","vhdl",
            "csound","latext","powerbasic","xml",
            "css","lisp","progress","yaml"
        };
        private ToolStrip toolStrip1;
        private ToolStripButton newToolStripButton;
        private ToolStripButton openToolStripButton;
        private ToolStripButton saveToolStripButton;
        private ToolStripButton printToolStripButton;
        private ToolStripSeparator toolStripSeparator;
        private ToolStripButton cutToolStripButton;
        private ToolStripButton copyToolStripButton;
        private ToolStripButton pasteToolStripButton;
        private ToolStripSeparator toolStripSeparator1;
        private ToolStripButton helpToolStripButton;
        private ScintillaNET.Scintilla scintillaEdit;
        private ToolStripComboBox toolStripComboBox_Language;
        private ToolStripButton toolStripButton1;
        private ToolStripButton toolStripButton2;
        private ToolStripButton toolStripButton3;
        private SplitContainer splitContainer1;
        private UCErrorWnd ucErrorWnd1;
        Single minfontsize;

        void scintilla1_MouseWheel(object sender, MouseEventArgs e)
        {
            /*
            var cursize = this.scintilla1.Font.Size + e.Delta/120 ;
            if(cursize < minfontsize)
            {
                cursize = minfontsize;
            }
            var font = new Font(this.scintilla1.Font.FontFamily,cursize );
            this.scintilla1.Font = font;
            this.scintilla1.Update();
            */
            

            //throw new NotImplementedException();
        }

        private void tspbtn_Compile_Click(object sender, EventArgs e)
        {
            
        }
        string _modulename;

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(UCSourceEdit));
            this.toolStrip1 = new System.Windows.Forms.ToolStrip();
            this.newToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.openToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.saveToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.printToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator = new System.Windows.Forms.ToolStripSeparator();
            this.cutToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.copyToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.pasteToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.toolStripButton1 = new System.Windows.Forms.ToolStripButton();
            this.toolStripButton2 = new System.Windows.Forms.ToolStripButton();
            this.toolStripButton3 = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
            this.toolStripComboBox_Language = new System.Windows.Forms.ToolStripComboBox();
            this.helpToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.scintillaEdit = new ScintillaNET.Scintilla();
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this.ucErrorWnd1 = new Plugin.SourceEdit.UCErrorWnd();
            this.toolStrip1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.scintillaEdit)).BeginInit();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            this.SuspendLayout();
            // 
            // toolStrip1
            // 
            this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.newToolStripButton,
            this.openToolStripButton,
            this.saveToolStripButton,
            this.printToolStripButton,
            this.toolStripSeparator,
            this.cutToolStripButton,
            this.copyToolStripButton,
            this.pasteToolStripButton,
            this.toolStripButton1,
            this.toolStripButton2,
            this.toolStripButton3,
            this.toolStripSeparator1,
            this.toolStripComboBox_Language,
            this.helpToolStripButton});
            this.toolStrip1.Location = new System.Drawing.Point(0, 0);
            this.toolStrip1.Name = "toolStrip1";
            this.toolStrip1.Size = new System.Drawing.Size(664, 25);
            this.toolStrip1.TabIndex = 0;
            this.toolStrip1.Text = "toolStrip1";
            // 
            // newToolStripButton
            // 
            this.newToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.newToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("newToolStripButton.Image")));
            this.newToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.newToolStripButton.Name = "newToolStripButton";
            this.newToolStripButton.Size = new System.Drawing.Size(23, 22);
            this.newToolStripButton.Text = "&New";
            // 
            // openToolStripButton
            // 
            this.openToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.openToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("openToolStripButton.Image")));
            this.openToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.openToolStripButton.Name = "openToolStripButton";
            this.openToolStripButton.Size = new System.Drawing.Size(23, 22);
            this.openToolStripButton.Text = "&Open";
            // 
            // saveToolStripButton
            // 
            this.saveToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.saveToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("saveToolStripButton.Image")));
            this.saveToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.saveToolStripButton.Name = "saveToolStripButton";
            this.saveToolStripButton.Size = new System.Drawing.Size(23, 22);
            this.saveToolStripButton.Text = "&Save";
            // 
            // printToolStripButton
            // 
            this.printToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.printToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("printToolStripButton.Image")));
            this.printToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.printToolStripButton.Name = "printToolStripButton";
            this.printToolStripButton.Size = new System.Drawing.Size(23, 22);
            this.printToolStripButton.Text = "&Print";
            // 
            // toolStripSeparator
            // 
            this.toolStripSeparator.Name = "toolStripSeparator";
            this.toolStripSeparator.Size = new System.Drawing.Size(6, 25);
            // 
            // cutToolStripButton
            // 
            this.cutToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.cutToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("cutToolStripButton.Image")));
            this.cutToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.cutToolStripButton.Name = "cutToolStripButton";
            this.cutToolStripButton.Size = new System.Drawing.Size(23, 22);
            this.cutToolStripButton.Text = "C&ut";
            // 
            // copyToolStripButton
            // 
            this.copyToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.copyToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("copyToolStripButton.Image")));
            this.copyToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.copyToolStripButton.Name = "copyToolStripButton";
            this.copyToolStripButton.Size = new System.Drawing.Size(23, 22);
            this.copyToolStripButton.Text = "&Copy";
            // 
            // pasteToolStripButton
            // 
            this.pasteToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.pasteToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("pasteToolStripButton.Image")));
            this.pasteToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.pasteToolStripButton.Name = "pasteToolStripButton";
            this.pasteToolStripButton.Size = new System.Drawing.Size(23, 22);
            this.pasteToolStripButton.Text = "&Paste";
            // 
            // toolStripButton1
            // 
            this.toolStripButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.toolStripButton1.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton1.Image")));
            this.toolStripButton1.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripButton1.Name = "toolStripButton1";
            this.toolStripButton1.Size = new System.Drawing.Size(23, 22);
            this.toolStripButton1.Text = "ShowLine";
            this.toolStripButton1.Click += new System.EventHandler(this.toolStripButton_ShowLine_Click);
            // 
            // toolStripButton2
            // 
            this.toolStripButton2.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.toolStripButton2.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton2.Image")));
            this.toolStripButton2.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripButton2.Name = "toolStripButton2";
            this.toolStripButton2.Size = new System.Drawing.Size(23, 22);
            this.toolStripButton2.Text = "Compile";
            this.toolStripButton2.Click += new System.EventHandler(this.toolStripButton_Compile_Click);
            // 
            // toolStripButton3
            // 
            this.toolStripButton3.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.toolStripButton3.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton3.Image")));
            this.toolStripButton3.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripButton3.Name = "toolStripButton3";
            this.toolStripButton3.Size = new System.Drawing.Size(23, 22);
            this.toolStripButton3.Text = "Run";
            this.toolStripButton3.Click += new System.EventHandler(this.toolStripButton_Run_Click);
            // 
            // toolStripSeparator1
            // 
            this.toolStripSeparator1.Name = "toolStripSeparator1";
            this.toolStripSeparator1.Size = new System.Drawing.Size(6, 25);
            // 
            // toolStripComboBox_Language
            // 
            this.toolStripComboBox_Language.Name = "toolStripComboBox_Language";
            this.toolStripComboBox_Language.Size = new System.Drawing.Size(121, 25);
            this.toolStripComboBox_Language.SelectedIndexChanged += new System.EventHandler(this.toolStripComboBox_Language_SelectedIndexChanged);
            // 
            // helpToolStripButton
            // 
            this.helpToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.helpToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("helpToolStripButton.Image")));
            this.helpToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.helpToolStripButton.Name = "helpToolStripButton";
            this.helpToolStripButton.Size = new System.Drawing.Size(23, 22);
            this.helpToolStripButton.Text = "He&lp";
            // 
            // scintillaEdit
            // 
            this.scintillaEdit.Dock = System.Windows.Forms.DockStyle.Fill;
            this.scintillaEdit.Location = new System.Drawing.Point(0, 0);
            this.scintillaEdit.Name = "scintillaEdit";
            this.scintillaEdit.Size = new System.Drawing.Size(664, 221);
            this.scintillaEdit.TabIndex = 1;
            // 
            // splitContainer1
            // 
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.Location = new System.Drawing.Point(0, 25);
            this.splitContainer1.Name = "splitContainer1";
            this.splitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal;
            // 
            // splitContainer1.Panel1
            // 
            this.splitContainer1.Panel1.Controls.Add(this.scintillaEdit);
            // 
            // splitContainer1.Panel2
            // 
            this.splitContainer1.Panel2.Controls.Add(this.ucErrorWnd1);
            this.splitContainer1.Size = new System.Drawing.Size(664, 394);
            this.splitContainer1.SplitterDistance = 221;
            this.splitContainer1.TabIndex = 2;
            // 
            // ucErrorWnd1
            // 
            this.ucErrorWnd1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.ucErrorWnd1.Location = new System.Drawing.Point(0, 0);
            this.ucErrorWnd1.Name = "ucErrorWnd1";
            this.ucErrorWnd1.Size = new System.Drawing.Size(664, 169);
            this.ucErrorWnd1.TabIndex = 0;
            // 
            // UCSourceEdit
            // 
            this.Controls.Add(this.splitContainer1);
            this.Controls.Add(this.toolStrip1);
            this.Name = "UCSourceEdit";
            this.Size = new System.Drawing.Size(664, 419);
            this.toolStrip1.ResumeLayout(false);
            this.toolStrip1.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.scintillaEdit)).EndInit();
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            this.splitContainer1.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void toolStripComboBox_Language_SelectedIndexChanged(object sender, EventArgs e)
        {

            this.scintillaEdit.ConfigurationManager.Language = this.toolStripComboBox_Language.Text;
        }

        private void toolStripButton_ShowLine_Click(object sender, EventArgs e)
        {
            if (scintillaEdit.Margins[0].Width == 20)
            {
                scintillaEdit.Margins[0].Width = 0;
            }
            else
            {
                scintillaEdit.Margins[0].Width = 20;
            }

        }
        static int i=0;
        PythonCompiler compiler = new PythonCompiler();

        private void toolStripButton_Compile_Click(object sender, EventArgs e)
        {
            
             _modulename = "test" + (i++).ToString();
             compiler.CompileSource(this.scintillaEdit.Text, _modulename);




            
        

        }

        private void toolStripButton_Run_Click(object sender, EventArgs e)
        {
            var dllpath = Directory.GetCurrentDirectory() + "\\" + _modulename;

            compiler.RunAssemble(dllpath);
            //wsanogh2 x = new wsanogh2();
            //try
            //{
            //    x.Initialize();
            //}
            //catch (System.Exception ex)
            //{
            //    Console.Write(ex.ToString());
            //}
            
        }
           
    
    }
}
